package com.wanxi.dao;

import com.wanxi.model.BrandModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Li
 * @Date 2023/4/26 16:03
 */
public class BrandDao {
    private static List<BrandModel> list = new ArrayList<BrandModel>();
    public int add(BrandModel model) {
//     实现id不唯一   随机数、毫秒数、UUID 、id自增（首先）
        int size = list.size();
        int id=1;
//        拿到最后一条数据id加1,size从0开始的，list.size()-1为最后一条
      if (size!=0){
          BrandModel model1 = list.get(size-1);
          id=model1.getId()+1;
      }
      model.setId(id);
        list.add(model);
        return 1;
    }

    public List<BrandModel> findAll() {
        return list;
    }


    public int deleteById(BrandModel model) {
        Iterator<BrandModel> iterator = list.iterator();
        while (iterator.hasNext()){
         BrandModel brandModel =  iterator.next();//得到当前的model
            if (brandModel.getId().equals(model.getId())){ //判断得到数据id与当前数据id是否相等
                iterator.remove();
            }
        }
        return 0;
    }

    public BrandModel findById(BrandModel brandModel) {
        for (int i = 0; i < list.size(); i++) {
            if (brandModel.getId().equals(list.get(i).getId())){
                return list.get(i);
            }

        }
        return null;
    }

    public int update(BrandModel model) {
        for (int i = 0; i < list.size(); i++) {
            if (model.getId().equals(list.get(i).getId())){
             list.set(i,model); //i拿到当前索引，覆盖model内容
            }
        }
        return 0;
    }
}
